package hiep.examandroidbasic;

import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	private SinhVienDataSource datasource;
	ListView lv = null;
	EditText editName, editAge, editCountry;
	List<SinhVienEntity> values;
	TextView tvSearch;
	int vitri;
	private Locale myLocale;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lv = (ListView) findViewById(android.R.id.list);
		editName = (EditText) findViewById(R.id.edtName);
		editAge = (EditText) findViewById(R.id.edtAge);
		editCountry = (EditText) findViewById(R.id.edtCountry);
		tvSearch = (TextView) findViewById(R.id.tvSearch);

		datasource = new SinhVienDataSource(this);
		datasource.open();
		values = datasource.getAllSinhVien();

		ArrayAdapter<SinhVienEntity> adapter = new ArrayAdapter<SinhVienEntity>(
				MainActivity.this, android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);

		MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
				R.layout.layout_list_sinhvien, values);
		lv.setAdapter(adapter2);

		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				vitri = arg2;
				tvSearch.setText(values.get(arg2).getId() + "");
				editName.setText(values.get(arg2).getName());
				editAge.setText(values.get(arg2).getAge() + "");
				editCountry.setText(values.get(arg2).getCountry());
			}

		});
	}

	public void RefreshListSV(View v) {
		datasource = new SinhVienDataSource(this);
		datasource.open();
		values = datasource.getAllSinhVien();

		ArrayAdapter<SinhVienEntity> adapter = new ArrayAdapter<SinhVienEntity>(
				MainActivity.this, android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);

		MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
				R.layout.layout_list_sinhvien, values);
		lv.setAdapter(adapter2);
		editAge.setText("");
		editName.setText("");
		editCountry.setText("");
		editName.requestFocus();
	}

	public void AddSinhVien(View v) {
		if (editName.getText() + "" == "" || editAge.getText() + "" == ""
				|| editCountry.getText() + "" == "") {
			Toast.makeText(MainActivity.this, "Hãy điền đầy đủ thông tin",
					Toast.LENGTH_SHORT).show();
		} else {
			long tuoi = Long.parseLong(editAge.getText() + "");
			if (tuoi < 16 || tuoi > 60) {
				Toast.makeText(MainActivity.this, "Hãy nhập tuổi từ 16 --> 60",
						Toast.LENGTH_SHORT).show();
			} else if (tuoi >= 16 && tuoi <= 60) {
				String name = editName.getText() + "";
				long age = Long.parseLong(editAge.getText() + "");
				String country = editCountry.getText() + "";

				@SuppressWarnings("unchecked")
				ArrayAdapter<SinhVienEntity> adapter = (ArrayAdapter<SinhVienEntity>) getListAdapter();
				SinhVienEntity sinhVienEntity = new SinhVienEntity();
				sinhVienEntity = datasource.createSinhVien(name, age, country);
				adapter.add(sinhVienEntity);

				Toast.makeText(MainActivity.this,
						"Thêm thành công: " + sinhVienEntity.getName(),
						Toast.LENGTH_SHORT).show();
				MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
						R.layout.layout_list_sinhvien, values);
				lv.setAdapter(adapter2);
				adapter.notifyDataSetChanged();

				editAge.setText("");
				editName.setText("");
				editCountry.setText("");
				editName.requestFocus();
			}
		}
	}

	public void UpdateSinhVien(View v) {
		try {
			String name = editName.getText() + "";
			String age = editAge.getText() + "";
			String country = editCountry.getText() + "";
			if (name == "" || age == "" || country == "") {
				Toast.makeText(MainActivity.this, "Hãy điền đầy đủ thông tin",
						Toast.LENGTH_SHORT).show();
			} else {
				long tuoi = Long.parseLong(editAge.getText() + "");
				if (tuoi < 16 || tuoi > 60) {
					Toast.makeText(MainActivity.this,
							"Hãy nhập tuổi từ 16 đến 60", Toast.LENGTH_SHORT)
							.show();
				} else if (tuoi >= 16 && tuoi <= 60) {
					SinhVienEntity sinhvienEntity = new SinhVienEntity();
					sinhvienEntity = (SinhVienEntity) getListAdapter().getItem(
							vitri);
					sinhvienEntity.setName(name);
					sinhvienEntity.setAge(Integer.parseInt(age));
					sinhvienEntity.setCountry(country);
					datasource.updateSinhVien(sinhvienEntity);

					MyArrayAdapter adapter = new MyArrayAdapter(
							MainActivity.this, R.layout.layout_list_sinhvien,
							values);
					lv.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					Toast.makeText(MainActivity.this,
							"Sửa thành công: " + sinhvienEntity.getName(),
							Toast.LENGTH_SHORT).show();

					editAge.setText("");
					editName.setText("");
					editCountry.setText("");
					editName.requestFocus();
				}
			}
		} catch (Exception e) {
			Toast.makeText(MainActivity.this,
					"Hãy chọn 1 đối tượng để cập nhật", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void DeleteSinhVien(View v) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				MainActivity.this);
		alertDialogBuilder.setTitle("Bạn có thực sự muốn xóa "
				+ values.get(vitri).getName() + " không?");
		alertDialogBuilder
				.setMessage("Click yes to delete!")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								@SuppressWarnings("unchecked")
								ArrayAdapter<SinhVienEntity> adapter = (ArrayAdapter<SinhVienEntity>) getListAdapter();
								SinhVienEntity sinhvienEntity = new SinhVienEntity();
								if (getListAdapter().getCount() > 0) {
									sinhvienEntity = (SinhVienEntity) getListAdapter()
											.getItem(vitri);
									datasource.deleteSinhVien(sinhvienEntity);
									adapter.remove(sinhvienEntity);
								}

								MyArrayAdapter adapter2 = new MyArrayAdapter(
										MainActivity.this,
										R.layout.layout_list_sinhvien, values);
								lv.setAdapter(adapter2);
								adapter.notifyDataSetChanged();

								Toast.makeText(
										MainActivity.this,
										"Xóa thành công: "
												+ sinhvienEntity.getName(),
										Toast.LENGTH_SHORT).show();

								editAge.setText("");
								editName.setText("");
								editCountry.setText("");
								editName.requestFocus();
							}
						})
				.setNeutralButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.show();
	}

	public void SearchSinhVien(View v) {
		String name = editName.getText() + "";
		String age = editAge.getText() + "";
		String country = editCountry.getText() + "";

		if (editName.getText() + "" == "" && editAge.getText() + "" == ""
				&& editCountry.getText() + "" == "") {
			Toast.makeText(MainActivity.this,
					"Hãy nhập 1 trong 3 tiêu chí trên", Toast.LENGTH_SHORT)
					.show();
		} else {
			if (name != "") {
				values = datasource.searchSinhVienByName(name);
				if (values.size() == 0) {
					Toast.makeText(MainActivity.this, "Không tìm thấy",
							Toast.LENGTH_SHORT).show();
				} else if (values.size() > 0) {
					ArrayAdapter<SinhVienEntity> adapter = new ArrayAdapter<SinhVienEntity>(
							MainActivity.this,
							android.R.layout.simple_list_item_1, values);
					setListAdapter(adapter);

					MyArrayAdapter adapter2 = new MyArrayAdapter(
							MainActivity.this, R.layout.layout_list_sinhvien,
							values);
					lv.setAdapter(adapter2);
				}
				editAge.setText("");
				editName.setText("");
				editCountry.setText("");
				editName.requestFocus();
			}
			if (age != "") {
				values = datasource.searchSinhVienByAge(age);
				if (values.size() == 0) {
					Toast.makeText(MainActivity.this, "Không tìm thấy",
							Toast.LENGTH_SHORT).show();
				} else if (values.size() > 0) {
					ArrayAdapter<SinhVienEntity> adapter = new ArrayAdapter<SinhVienEntity>(
							MainActivity.this,
							android.R.layout.simple_list_item_1, values);
					setListAdapter(adapter);

					MyArrayAdapter adapter2 = new MyArrayAdapter(
							MainActivity.this, R.layout.layout_list_sinhvien,
							values);
					lv.setAdapter(adapter2);
				}
				editAge.setText("");
				editName.setText("");
				editCountry.setText("");
				editName.requestFocus();
			}
			if (country != "") {
				values = datasource.searchSinhVienByCountry(country);
				if (values.size() == 0) {
					Toast.makeText(MainActivity.this, "Không tìm thấy",
							Toast.LENGTH_SHORT).show();
				} else if (values.size() > 0) {
					ArrayAdapter<SinhVienEntity> adapter = new ArrayAdapter<SinhVienEntity>(
							MainActivity.this,
							android.R.layout.simple_list_item_1, values);
					setListAdapter(adapter);

					MyArrayAdapter adapter2 = new MyArrayAdapter(
							MainActivity.this, R.layout.layout_list_sinhvien,
							values);
					lv.setAdapter(adapter2);
				}
				editAge.setText("");
				editName.setText("");
				editCountry.setText("");
				editName.requestFocus();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		int itemId = 113;
		menu.add(0, itemId, 0, "English");
		itemId = 114;
		menu.add(0, itemId, 1, "Tiếng Việt");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 113:
			Toast.makeText(this, "English Interface", Toast.LENGTH_SHORT)
					.show();
			setLocale("en");
			break;
		case 114:
			Toast.makeText(this, "Giao diện Tiếng Việt", Toast.LENGTH_SHORT)
					.show();
			setLocale("vi");
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void setLocale(String lang) {
		myLocale = new Locale(lang);
		Resources res = getResources();
		DisplayMetrics dm = res.getDisplayMetrics();
		Configuration conf = res.getConfiguration();
		conf.locale = myLocale;
		res.updateConfiguration(conf, dm);
		Intent refresh = new Intent(this, MainActivity.class);
		startActivity(refresh);
	}
}
